package com.yudaob2b.elong.controller.domestic.data;

import com.yudaob2b.business.service.ElongHotelListService;
import com.yudaob2b.business.service.ElongHotelRateService;
import com.yudaob2b.business.service.RedisLockService;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.common.utils.ListUtil;
import com.yudaob2b.common.utils.PageUtil;
import com.yudaob2b.elong.constant.Constant;
import com.yudaob2b.elong.service.HotelRateService;
import com.yudaob2b.elong.service.HttpStaticService;
import com.yudaob2b.elong.task.DataRateLocalTask;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 国内酒店-产品价格
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/15/18 4:57 PM
 */
@RestController
@RequestMapping("/api/data/rate")
public class ApiDataRateController {
    private static Logger dataRateLogger = LoggerFactory.getLogger("dataRateLogger");
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    // redis业务锁key
    private final String REDIS_LOCK_KEY_RATE_LOCAL = "lock_rate_local";

    @Autowired
    private HttpStaticService httpStaticService;

    @Autowired
    private ElongHotelListService elongHotelListService;

    @Autowired
    private ElongHotelRateService elongHotelRateService;

    @Autowired
    private RedisLockService redisLockService;

    @Autowired
    private HotelRateService hotelRateService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource(name = "dataRateLocalTaskExecutor")
    private ThreadPoolTaskExecutor dataRateLocalTaskExecutor;

    @Resource(name = "dataRateDbTaskExecutor")
    private ThreadPoolTaskExecutor dataRateDbTaskExecutor;

    /**
     * 全量落地
     */
    @RequestMapping("/local")
    public void go() {
        dataRateLogger.info("启动艺龙国内酒店－产品价格全量，开始时间：{} ", DateUtil.format(new Date(), DateUtil.PATTERN_CLASSICAL));
        long start = System.currentTimeMillis();

        // 重置增量的参数，当前时间5分钟前
        hotelRateService.updateIncrLastIdInfo("", DateUtil.rollDate(new Date(), 0, -Constant.INCR_AHEAD_MINUTE));
        try {
            // redis业务锁
            if (redisLockService.lock(REDIS_LOCK_KEY_RATE_LOCAL, 0)) {
                int rateSearchSize = 10;

                // 查询酒店id参数集合
                Map<String, Object> condition = new HashMap<>();
                condition.put("status", "0");
                int page = 1;
                int pageSize = 10000;

                while (true) {
                    try {
                        PageUtil.setPage(page, pageSize, condition);
                        List<String> hotelIdList = elongHotelListService.queryHotelIdListPage(condition);
                        if (hotelIdList.size() == 0) {
                            break;
                        }

                        List<List<String>> pageList = ListUtil.split(hotelIdList, rateSearchSize);
                        for (List<String> pageHotelIdList : pageList) {
                            DataRateLocalTask dataRateLocalTask = new DataRateLocalTask(
                                    pageHotelIdList, httpStaticService
                                    , hotelRateService, dataRateDbTaskExecutor
                                    , elongHotelRateService, redisTemplate
                                    , ElasticSearchHelper.getRestClientUtil()
                            );
                            dataRateLocalTaskExecutor.execute(dataRateLocalTask);
                        }
                    } catch (Exception e) {
                        dataRateLogger.info("全量同步产品价格异常：", e);
                        e.printStackTrace();
                    } finally {
                        page++;
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            redisLockService.unlock(REDIS_LOCK_KEY_RATE_LOCAL);
        }

        dataRateLogger.info("完成艺龙国内酒店－产品价格全量，当前时间：{}，一共耗时：{} ", DateUtil.format(new Date(), DateUtil.PATTERN_CLASSICAL), (System.currentTimeMillis() - start));
    }

}
